﻿using PasswdTool.Model;
using PasswdTool.Properties;
using System;
using System.Windows.Forms;
using Tables;

namespace PasswdTool
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            LoadMenu();
            LoadTable();
            if (GlobalContainer.CurrentUser == null)
            {
                LoginFrm loginFrm = new LoginFrm();
                loginFrm.ShowDialog();
            }
            if (GlobalContainer.CurrentUser != null)
            {
                LoadData();
            }

        }
        private void LoadTable()
        {
            if (!GlobalContainer.IsExists(GlobalContainer.userTable))
            {
                new UserTable().Serialize();
            }
            if (!GlobalContainer.IsExists(GlobalContainer.passwdTable))
            {
                new PasswdTable().Serialize();
            }
            GlobalContainer.UserTable = UserTable.Deserialize();
            if (GlobalContainer.UserTable == null)
            {
                GlobalContainer.UserTable = new UserTable();
            }
            GlobalContainer.PasswdTable = PasswdTable.Deserialize();
            if (GlobalContainer.PasswdTable == null)
            {
                GlobalContainer.PasswdTable = new PasswdTable();
            }
        }
        public void LoadData()
        {
            PasswdTable table = GlobalContainer.PasswdTable;
            UserModel currentUser = GlobalContainer.CurrentUser;
            PasswdModel query = new PasswdModel()
            {
                UserId = currentUser.Id
            };
            var passwdList = table.Query(query);
            foreach (var item in passwdList)
            {
                int index = dataGridView1.Rows.Add(item.Name, item.Username, "●●●●●●");
                dataGridView1.Rows[index].Tag = item;
            }
        }
        private void LoadMenu()
        {
            ToolStripItem newToolStripItem = mainToolStrip.Items.Add("新建");
            newToolStripItem.Image = Resources.add;
            newToolStripItem.Click += new EventHandler((o, te) =>
            {
                DetailFrm detailFrm = new DetailFrm
                {
                    IsNew = true,
                    MainFrm = this
                };
                detailFrm.ShowDialog();
            });
            ToolStripItem editToolStripItem = mainToolStrip.Items.Add("编辑");
            editToolStripItem.Image = Resources.edit;
            editToolStripItem.Click += new EventHandler((o, te) =>
            {
                if (dataGridView1.SelectedRows.Count == 0)
                {
                    MessageBox.Show("未选择账号信息!", "PasswdTool提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                DetailFrm detailFrm = new DetailFrm
                {
                    IsNew = false,
                    IsUpdate = true,
                    MainFrm = this,
                    PasswdModel = (PasswdModel)dataGridView1.SelectedRows[0].Tag
                };
                detailFrm.ShowDialog();
            });
        }

        private void DataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                DetailFrm detailFrm = new DetailFrm
                {
                    IsNew = false,
                    MainFrm = this,
                    PasswdModel = (PasswdModel)dataGridView1.SelectedRows[0].Tag
                };
                detailFrm.ShowDialog();
            }

        }

        private void MainFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            GlobalContainer.UserTable.Serialize();
            GlobalContainer.PasswdTable.Serialize();
        }
    }
}
